// counterStore.js 或 counterStore.ts
import { defineStore } from 'pinia'

export const useStore = defineStore('counter', {
  // 状态
  state: () => ({
    count:0,

    user:{
      name: "",
      token: "",
    },
    id:0,
  }),

  // 计算属性（getter）
  getters: {
    doubleCount: (state) => {
      return state.count * 2
    }
  },

  // 方法（actions）
  actions: {
    increment() {
      this.count++
    },

    decrement() {
      this.count--
    },

    reset() {
      this.count = 0
    },
     initializeUserFromLocalStorage() {
     
     this.user.name = localStorage.getItem('name') || "";
     this.user.token = localStorage.getItem('token') || "";
    },
  


        // 示例：异步操作
    async fetchData() {
      // 假设的异步操作，如从API获取数据
      // 这里只是一个示例，实际应用中需要替换为真实逻辑
      await new Promise(resolve => setTimeout(resolve, 1000))
      this.count = Math.floor(Math.random() * 100)
    }
  }
})